package com.egene.tw.activity.product;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.egene.tw.Constant;
import com.egene.tw.R;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.model.Category;
import com.egene.tw.model.net.ResponseBean;
import com.egene.tw.utils.ImageUtil;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.egene.tw.utils.TitleUtil;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

@EActivity(R.layout.activity_category_info)
public class CategoryInfoActivity extends BaseActivity {
    public static final String ID = "id";
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;


    @ViewById
    ImageView ivImg;
    @ViewById
    TextView tvContent;
    @ViewById
    WebView webView;


    @AfterViews
    void initViews(){
        initToolBar();
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.detail);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Click(R.id.rl_call)
    void call(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + Constant.CONNECT_PHONE));
        startActivity(intent);
    }

    @Click(R.id.rl_contact)
    void contact(){
        ContactOnlineActivity_.intent(this).start();
    }

    @AfterInject
    void deal() {
        String id = getIntent().getStringExtra(ID);
        EgeneApi.getProductInfo(id, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseBean<Category> result = JsonUtil.toBean(responseBody,
                        new TypeToken<ResponseBean<Category>>() {
                        }.getType());
                if(result != null){
                    if(result.isSuccess()){
                        updateUI(result.getData());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }

        });
    }

    private void updateUI(Category category){
//        if (!TextUtils.isEmpty(category.getImgUrl())) {
        ImageUtil.loadImage(this, ivImg, category.getImg_url());
//        }
//
//        if(TextUtils.isEmpty(article.getImg_url())){
//            Picasso.with(this).load(R.drawable.pic_product_defualt).into(ivImg);
//        }else{
//            Picasso.with(this).load(EgeneApi.BASE_URL + article.getImg_url()).placeholder(R.drawable.pic_product_defualt).into(ivImg);
//        }
        mToolBarTitle.setText(TitleUtil.getTitle(category.getTitle()));
//        tvContent.setText(Html.fromHtml(article.getContent()));
        webView.getSettings().setDefaultTextEncodingName("UTF-8") ;
        webView.loadData(category.getContent(),"text/html; charset=UTF-8", "UTF-8");




//        if(TextUtils.isEmpty(category.getImg_url())){
//            Picasso.with(this).load(R.drawable.pic_product_defualt).into(ivImg);
//        }else{
//            Picasso.with(this).load(category.getImg_url()).placeholder(R.drawable.pic_product_defualt).into(ivImg);
//        }
        ImageUtil.loadImage(this,ivImg,category.getImg_url());
        mToolBarTitle.setText(TitleUtil.getTitle(category.getTitle()));
        tvContent.setText(Html.fromHtml(category.getContent()));
    }
}
